import 'package:flutter/material.dart';
import 'package:news/com.comm/contants/constant.dart';

/**
 * 全局设置弹窗 item 条目 界面
 */
class TranslateGlobalSetItem extends StatefulWidget {
  final dynamic data;
  final int backIndex;
  final Function backData;

  TranslateGlobalSetItem(
      {Key? key,
      required this.data,
      required this.backData,
      required this.backIndex})
      : super(key: key);

  @override
  _TranslateGlobalSetItemState createState() => _TranslateGlobalSetItemState();
}

class _TranslateGlobalSetItemState extends State<TranslateGlobalSetItem> {
  final TextEditingController _controllerName = TextEditingController(); //语种名称
  final TextEditingController _controllerShortName =
      TextEditingController(); //语言简称
  final TextEditingController _controllerValuesName =
      TextEditingController(); //语言values 后缀
  final TextEditingController _controllerApiName =
      TextEditingController(); //语言翻译Api简称
  final TextEditingController _controllerErrorName =
      TextEditingController(); //语言故障码名称
  late bool _isNeedTranslate; //语言是否需要翻译
  Map<dynamic, dynamic> transMap = {}; // 要预览的多语言数据

  @override
  void initState() {
    super.initState();
    setState(() {
      transMap = widget.data;
      _controllerName.text = transMap[Constant.name]==null?"":transMap[Constant.name];
      _controllerShortName.text = transMap[Constant.shortName]==null?"":transMap[Constant.shortName];
      _controllerValuesName.text = transMap[Constant.valuesName]==null?"":transMap[Constant.valuesName];
      _controllerApiName.text = transMap[Constant.apiName]==null?"":transMap[Constant.apiName];
      _controllerErrorName.text = transMap[Constant.errorName]==null?"":transMap[Constant.errorName];
      _isNeedTranslate = transMap[Constant.isNeedTranslate]==null?false:transMap[Constant.isNeedTranslate];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(children: <Widget>[
      _getEdit(_controllerName, Constant.name),

      _getEdit(_controllerShortName, Constant.shortName),
      _getEdit(_controllerValuesName, Constant.valuesName),
      _getEdit(_controllerApiName, Constant.apiName),
      _getEdit(_controllerErrorName, Constant.errorName),
      Flexible(
          child: Checkbox(
              value: _isNeedTranslate,
              onChanged: (change) {
                debugPrint("change:$change");
                setState(() {
                  _isNeedTranslate = change!;
                  transMap[Constant.isNeedTranslate] = _isNeedTranslate;
                  widget.backData(transMap, widget.backIndex);
                });
              }),
          flex: 1,
          fit: FlexFit.tight)
    ]);
  }

  Widget _getEdit(TextEditingController textController, String key) {
    return Flexible(
        child: Padding(padding:EdgeInsets.only(right: 8),child: Container(
            child: TextField(
              decoration: InputDecoration(
                    hintText: "多语言配置",
                    border: OutlineInputBorder(),
              ),
              onChanged: (value) {
                setState(() {
                  transMap[key] = value;
                  widget.backData(transMap, widget.backIndex);
                });
              },
              controller: textController,
            ),
            alignment: Alignment.center)),
        flex: 1,
        fit: FlexFit.tight);
  }

  @override
  void dispose() {
    super.dispose();
    _controllerName.dispose();
    _controllerShortName.dispose();
    _controllerValuesName.dispose();
    _controllerApiName.dispose();
    _controllerErrorName.dispose();
  }
}
